package com.lcq.study.leetcode;

/**
 * @ClassName T20220329
 * @Description
 * @Author lichuanqi
 * @Date 2022/3/29 19:00
 * @Version 1.0
 **/
public class T20220329 {
    public static void main(String[] args) {
        int a[] = {1, 1, 2, 2, 3,3,3,3,3,4};
        int length = removeDuplicates1(a);
        for (int i = 0; i < length; i++) {
            System.out.println(a[i]);
        }
    }

    public static int removeDuplicates(int[] nums) {
        int length = nums.length;
        int i = 0;
        int j = i;
        while(i<length && j<length){
            if(nums[i] == nums[j]){
                j++;
            }else{
                //整体搬移这里可以优化
                for(int k = j, p=i+1; k<length; k++,p++){
                    nums[p] = nums[k];
                }
                length = length - (j - i - 1);
                i++;
                j = i;
            }
        }
        return i+1;
    }
    /**
     * @Author lichuanqi
     * @Description 双指针
     * @Date 19:08 2022/3/29
     * @Param [nums]
     * @return int
     **/
    public static int removeDuplicates1(int[] nums) {
        int length = nums.length;
        if (length == 0){
            return length;
        }
        int slow = 1;
        int fast = 1;
        while(fast<length){
          if (nums[fast-1] != nums[fast]){
              nums[slow] = nums[fast];
              slow++;
          }
          fast++;
        }
        return slow;
    }
}
